package com.example.customviewpager;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import java.util.HashMap;

/**
 * Created by ThinkPad on 2017/3/25.
 */

public class CustomViewpager extends ViewPager {
    private View mLeftView;
    private View mRightView;
    private HashMap<Integer, ImageView> childMaps = new HashMap<>();

    public CustomViewpager(Context context) {
        super(context);
    }

    public CustomViewpager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void addChildView(ImageView imageView, int position) {
        childMaps.put(position, imageView);
    }
    public void removeChildView(int position) {
        childMaps.remove(position);
    }

    @Override
    protected void onPageScrolled(int position, float offset, int offsetPixels) {
        super.onPageScrolled(position, offset, offsetPixels);

        mLeftView = childMaps.get(position);
        mRightView = childMaps.get(position + 1);
        if (mRightView != null) {
            startLaunchAnimation(mLeftView,mRightView,position,offset,offsetPixels);
        }
        if (mLeftView != null) {
            mLeftView.bringToFront();
        }
    }

    private void startLaunchAnimation(View leftView, View rightView, int position, float offset, int offsetPixels) {
        rightView.setAlpha(offset);
        rightView.setTranslationX(-(rightView.getWidth() - offsetPixels));
        rightView.setScaleX(offset);
        rightView.setScaleY(offset);
    }
}
